package cn.itcast.n4;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.TestWaitNotify")
public class TestWaitNotify {

    final static Object lock = new Object();

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            synchronized (lock){
                log.debug("执行...");
                try {
                    lock.wait();
//                    lock.wait(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.debug("其他代码...");
            }
        },"t1").start();

        new Thread(() -> {
            synchronized (lock){
                log.debug("执行...");
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.debug("其他代码...");
            }
        },"t2").start();

        Thread.sleep(500L);
        log.debug("唤醒 lock 上其他线程");
        synchronized (lock){
//            lock.notify();//唤醒一个线程
            log.debug("看看是否是释放了自己的锁，才唤醒其他线程。。。");
            lock.notifyAll();//唤醒的锁对象上等待的所有线程
        }
    }
}
